import libraries¶

In [23]:
import cv2
import numpy as np
from matplotlib import pyplot as plt

read image¶

In [24]:
image = cv2.imread('task1.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  #convert to gray scale

1- Blur Then Salt And Pepper Noise.¶

In [25]:
#apply blur on image
blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)

 # Define probability
noise_prob = 0.05                

# Add salt and pepper noise
salt_pepper_noise = np.zeros_like(gray_image)
salt_pepper_noise[np.random.random(gray_image.shape) < noise_prob] = 255
salt_pepper_noise[np.random.random(gray_image.shape) < noise_prob] = 0

# Combine noise with the grayscale image
noisy_image = cv2.add(blurred_image, salt_pepper_noise)

#show image
fig = plt.figure(figsize=(15, 20))
ax1 = fig.add_subplot(1,1,1)
ax1.imshow(noisy_image, cmap='gray')
ax1.title.set_text('Blur Then Salt And Pepper Noise')

2-Threshold Then Salt And Pepper Noise.¶

In [26]:
#thresholding
_, thresholded_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)

 # Define probability
noise_prob = 0.05                

# Add salt and pepper noise
salt_pepper_noise = np.zeros_like(gray_image)
salt_pepper_noise[np.random.random(gray_image.shape) < noise_prob] = 255
salt_pepper_noise[np.random.random(gray_image.shape) < noise_prob] = 0

# Combine noise with the grayscale image
noisy_image = cv2.add(thresholded_image, salt_pepper_noise)

#show image
fig = plt.figure(figsize=(15, 20))
ax1 = fig.add_subplot(1,1,1)
ax1.imshow(noisy_image, cmap='gray')
ax1.title.set_text('Threshold Then Salt And Pepper Noise')

3-Threshold Then Blur.¶

In [27]:
#thresholding
_, thresholded_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)

#apply blur on image
blurred_image = cv2.GaussianBlur(thresholded_image, (5, 5), 0)

#show image
fig = plt.figure(figsize=(15, 20))
ax1 = fig.add_subplot(1,1,1)
ax1.imshow(blurred_image, cmap='gray')
ax1.title.set_text('Threshold Then Blur')

4-Threshold, Blur Then Salt And Pepper Noise.¶

In [28]:
#thresholding
_, thresholded_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)

#apply blur on image
blurred_image = cv2.GaussianBlur(thresholded_image, (5, 5), 0)

 # Define probability
noise_prob = 0.05                

# Add salt and pepper noise
salt_pepper_noise = np.zeros_like(gray_image)
salt_pepper_noise[np.random.random(gray_image.shape) < noise_prob] = 255
salt_pepper_noise[np.random.random(gray_image.shape) < noise_prob] = 0

# Combine noise with the grayscale image
noisy_image = cv2.add(blurred_image, salt_pepper_noise)

#show image
fig = plt.figure(figsize=(15, 20))
ax1 = fig.add_subplot(1,1,1)
ax1.imshow(noisy_image, cmap='gray')
ax1.title.set_text('Threshold, Blur Then Salt And Pepper Noise')

5-Apply 3 Types of Blurring filters¶

In [22]:
##################################        1

# normal blur filter 
blur_image = cv2.blur(gray_image, (5, 5))



##################################      2

#median blur 
blur_median_image = cv2.medianBlur(gray_image, 5)



##################################           3

#guassian blur 
gass_blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)



##################################             4

# Apply bilateral filter with diameter 9, sigmaColor 75, and sigmaSpace 75
bi_blurred_image = cv2.bilateralFilter(gray_image, 9, 75, 75)



###################################################################################################################


#show image
fig = plt.figure(figsize=(15, 20))
ax1 = fig.add_subplot(2,2,1)
ax1.imshow(blur_image, cmap='gray')
ax1.title.set_text('normal blur')

ax2 = fig.add_subplot(2,2,2)
ax2.imshow(blur_median_image, cmap='gray')
ax2.title.set_text('median blur')

ax3 = fig.add_subplot(2,2,3)
ax3.imshow(gass_blurred_image, cmap='gray')
ax3.title.set_text('guassian blur')

ax4 = fig.add_subplot(2,2,4)
ax4.imshow(bi_blurred_image, cmap='gray')
ax4.title.set_text('bilateral')
In [ ]: